package com.xiaoai.timedtask.quartz;

import org.quartz.*;

import java.util.Date;

/**
 * quartz任务
 * 异步执行 ，不同的job实例
 *
 * @Author
 * @Date 2021-08-30 23:00
 */
public class MyJob implements Job {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void execute(JobExecutionContext contex) throws JobExecutionException {
        System.out.println("quartz myjob date: "+new Date());

        // 获取job传递参数
        JobDataMap jobDetailJDM = contex.getJobDetail().getJobDataMap();
        System.out.println("jobDetailPrams:["+jobDetailJDM.get("jobDetail"));
        System.out.println(jobDetailJDM.get("name1"));
        System.out.println(jobDetailJDM.get("name2"));

        // 获取trigger传递参数
        JobDataMap triggerJDM = contex.getTrigger().getJobDataMap();
        System.out.println("triggerPrams:[" + triggerJDM.get("trigger"));

        // 获取job和trigger合并后的传递参数；问题：相同的key会覆盖
        JobDataMap mergedJDM = contex.getMergedJobDataMap();
        System.out.println("mergedJDMPrams:" + mergedJDM.get("jobDetail"));
        System.out.println("mergedJDMPrams:" + mergedJDM.get("trigger"));
        System.out.println("mergedJDMPrams - mergedAttr:" + mergedJDM.get("mergedAttr"));

        System.out.println("by attribute set method:" + name);

    }

}
